package ssl.gui;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import ssl.speech.Conversation;
import ssl.speech.Line;
import ssl.speech.Tag;

public class Gui {
	Composite contentPanel;
	Table table;
	Combo tags;
	ArrayList<Line> lines = new ArrayList<Line>();

	Conversation crtConversation;
	int crtSelection = 0;

	public Gui(Shell parent) {

		contentPanel = new Composite(parent, SWT.BORDER);
		parent.setLayout(new FillLayout());
		parent.setText("SPEECH ACTS - Manual Tagger");

		contentPanel.setLayout(new GridLayout(3, true));
		table = new Table(contentPanel, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL
				| SWT.H_SCROLL);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.horizontalSpan = 2;
		data.verticalSpan = 3;
		table.setLayoutData(data);

		table.setLinesVisible(true);

		String columns[] = { "ID", "REF", "TAG", "UTTERANCE" };
		int colWidths[] = { 50, 50, 100, 250 };

		for (int i = 0; i < columns.length; i++) {
			TableColumn c = new TableColumn(table, SWT.NONE);
			c.setWidth(colWidths[i]);
			c.setText(columns[i]);
		}
		table.setHeaderVisible(true);

		Button loadButton = new Button(contentPanel, SWT.PUSH);
		loadButton.setText("Load file");
		loadButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		loadButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				table.removeAll();
				crtSelection = 0;
				lines.clear();

				FileDialog fd = new FileDialog(Display.getCurrent()
						.getActiveShell(), SWT.OPEN);
				fd.setText("Open");
				String[] filterExt = { "*.xml", "*.*" };
				fd.setFilterExtensions(filterExt);
				String selected = fd.open();

				crtConversation = new Conversation(selected);

				lines = crtConversation.getLines();

				for (int i = 0; i < lines.size(); i++) {
					TableItem item = new TableItem(table, SWT.NONE);

					item.setData(new Integer(i));

					Line li = lines.get(i);
					String info[] = li.getInfo();
					for (int j = 0; j < info.length; j++)
						item.setText(j, info[j]);
				}

				TableItem item = table.getItem(crtSelection);
				tags.select(Tag.getIndexOf(item.getText(2)));
			}
		});

		table.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				tags.select(0);
				crtSelection = (Integer) event.item.getData();
			}
		});

		Button saveButton = new Button(contentPanel, SWT.PUSH);
		saveButton.setText("Save current tagging");
		saveButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		saveButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				crtConversation.save();
				System.out.println("Tagging saved in "
						+ crtConversation.getSavedFile());
			}
		});

		tags = new Combo(contentPanel, SWT.NONE);
		tags.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		tags.setItems(Tag.values);

		tags.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {

				TableItem tItem = table.getItem(crtSelection);
				int index = tags.getSelectionIndex();
				tItem.setText(2, Tag.values[index]);
				crtConversation.setTag(crtSelection + 1, Tag.values[index]);

				table.deselect(crtSelection);
				crtSelection++;
				table.select(crtSelection);
				tags.select(0);
			}
		});

	}
}
